package com.webchat.ugc.service.mall;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.exception.BusinessException;
import com.webchat.domain.vo.request.mall.MallProductStockOperateReqVO;
import com.webchat.domain.vo.request.mall.MallProductStockQueryReqVO;
import com.webchat.rmi.pgc.mall.MallProductStockClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class MallProductStockService {


    @Autowired
    private MallProductStockClient mallProductStockClient;

    /**
     * 商品库存预扣减
     *
     * @param spuId
     * @param skuId
     * @param num
     * @return
     */
    public boolean reduceStock(Long spuId, Long skuId, Integer num) {

        MallProductStockOperateReqVO mallProductStockOperateReqVO = MallProductStockOperateReqVO.builder()
                .spuId(spuId)
                .skuId(skuId)
                .num(num)
                .build();
        APIResponseBean<Boolean> responseBean = mallProductStockClient.reduceStock(mallProductStockOperateReqVO);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 库存回滚/释放（库存增加）
     *
     * @param spuId
     * @param skuId
     * @param num
     * @return
     */
    public boolean rollback(Long spuId, Long skuId, Integer num) {

        MallProductStockOperateReqVO mallProductStockOperateReqVO = MallProductStockOperateReqVO.builder()
                                                                    .spuId(spuId)
                                                                    .skuId(skuId)
                                                                    .num(num)
                                                                    .build();
        try {
            APIResponseBean<Boolean> responseBean = mallProductStockClient.incrementStock(mallProductStockOperateReqVO);
            if (APIResponseBeanUtil.isOk(responseBean)) {
                return responseBean.getData();
            }
            throw new BusinessException(responseBean.getMsg());
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 查询商品库存
     *
     * @param spuId
     * @param skuId
     * @return
     */
    public Integer getStock(Long spuId, Long skuId) {

        APIResponseBean<Integer> responseBean = mallProductStockClient.getStock(spuId, skuId);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }


    /**
     * 批量查询商品库存
     *
     * @param spuId
     * @param skuIdList
     * @return
     */
    public Map<Long, Integer> batchGetStock(Long spuId, List<Long> skuIdList) {

        MallProductStockQueryReqVO mallProductStockQueryReq = MallProductStockQueryReqVO.builder()
                                                                .spuId(spuId)
                                                                .skuIdList(skuIdList)
                                                                .build();
        APIResponseBean<Map<Long, Integer>> responseBean = mallProductStockClient.batchGetStock(mallProductStockQueryReq);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }
}
